package com.scwatch.core.domain;

import com.google.common.base.Objects;
import com.scwatch.core.domain.NodeAnchor.NodeAnchorId;
import javax.persistence.*;
import java.io.Serializable;

/**
 * NodeAnchor
 * 
 * @author gjl
 * 
 */
@Entity
@Table(name = "cms_node_anchor")
@IdClass(NodeAnchorId.class)
public class NodeAnchor implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "f_node_id", nullable = false)
	private Node node;

	@Id
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "f_anchor_id", nullable = false)
	private Node anchor;

	public NodeAnchor() {
	}

	public NodeAnchor(Node node, Node anchor) {
		this.node = node;
		this.anchor = anchor;
	}

	public Node getNode() {
		return node;
	}

	public void setNode(Node node) {
		this.node = node;
	}

	public Node getAnchor() {
		return anchor;
	}

	public void setAnchor(Node anchor) {
		this.anchor = anchor;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof NodeAnchor)) {
			return false;
		}
		NodeAnchor that = (NodeAnchor) o;
		return Objects.equal(node, that.node) && Objects.equal(anchor, that.anchor);
	}

	@Override
	public int hashCode() {
		return Objects.hashCode(node, anchor);
	}

	public static class NodeAnchorId implements Serializable {
		private static final long serialVersionUID = 1L;

		Integer node;
		Integer anchor;

		public NodeAnchorId() {
		}

		public NodeAnchorId(Integer nodeId, Integer anchorId) {
			this.node = nodeId;
			this.anchor = anchorId;
		}

		@Override
		public boolean equals(Object o) {
			if (this == o) {
				return true;
			}
			if (!(o instanceof NodeAnchorId)) {
				return false;
			}
            NodeAnchorId that = (NodeAnchorId) o;
			return Objects.equal(node, that.node) && Objects.equal(anchor, that.anchor);
		}

		@Override
		public int hashCode() {
			return Objects.hashCode(node, anchor);
		}
	}
}
